package com.example.minzhengtong.ui.add

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.minzhengtong.Const
import com.example.minzhengtong.R
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.data.*
import com.example.minzhengtong.databinding.CommonListBinding
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.SpUtil
import com.example.minzhengtong.utils.UIUtils
import com.example.minzhengtong.utils.WUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import okhttp3.*
import java.io.IOException


/**
 * created by WY on 2021/6/17.
 */
class SocialFG : BaseFg() {

    companion object {
        @JvmStatic
        fun newInstance(tag: Int = 0) =
            SocialFG().apply {
                arguments = Bundle().apply {
                    putInt("tag", tag)
                }
            }
    }

    private lateinit var binding : CommonListBinding

    private val userBean by lazy {
        SpUtil.getInstance().getObject(Const.USER_BEAN) as UserBean
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = CommonListBinding.inflate(inflater)
        return binding.root
    }

    private val mData1 = mutableListOf<HistoryBean.DataBean.ListBean>()
    private val mAdapter1 by lazy {
        SocialAdapter1()
    }
    private val mData2 = mutableListOf<HistoryBean.DataBean.ListBean>()
    private val mAdapter2 by lazy {
        SocialAdapter2()
    }
    private val mData3 = mutableListOf<HistoryBean.DataBean.ListBean>()
    private val mAdapter3 by lazy {
        SocialAdapter3()
    }

    private val pageTag by lazy {
        arguments!!.getInt("tag")!!
    }

    override fun initView() {
        /*detail(
            (baseContext as SocialInsuranceA).getStartTime(),
            (baseContext as SocialInsuranceA).getEndTime()
        )*/

        qyzgBean.addAll( Gson().fromJson(userBean.data.qyzgylx,
            object : TypeToken<List<QyzgylxBean>>() {}.type))
        LogUtil.d("企业职工保险${userBean.data.qyzgylx}")
        LogUtil.d("企业职工保险${qyzgBean}")

        syBean.addAll(Gson().fromJson(userBean.data.sybx,
            object : TypeToken<List<SybxBean>>() {}.type))
        LogUtil.d("失业保险${userBean.data.sybx}")
        LogUtil.d("失业保险${syBean}")

        gsBean.addAll(Gson().fromJson(userBean.data.gsbx,
            object : TypeToken<List<GsbxBean>>() {}.type))
        LogUtil.d("工伤保险${userBean.data.gsbx}")
        LogUtil.d("工伤保险${gsBean}")
        binding.recyclerView.layoutManager = WUtils.verManager(baseContext)
        when(pageTag){
            0 -> {
                binding.recyclerView.adapter = mAdapter1
            }
            1 -> {
                binding.recyclerView.adapter = mAdapter2
            }
            2 -> {
                binding.recyclerView.adapter = mAdapter3
            }
        }
    }

    override fun initListener() {

    }

    private val qyzgBean = mutableListOf<QyzgylxBean>()
    private val syBean = mutableListOf<SybxBean>()
    private val gsBean = mutableListOf<GsbxBean>()

    override fun lazyLoad() {

    }

    /*private fun detail(start: String, end: String){
        mData1.clear()
        mData2.clear()
        mData3.clear()
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
            .add("token", SpUtil.getInstance().getString(Const.TOKEN))
            .add("start", start)
            .add("end", end)
            .build()
        val request = Request.Builder()
            .url(Const.history)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                UIUtils.showToastSafely("网络连接失败")
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val json = response.body()!!.string()
                LogUtil.d("获取列表数据$json")
                val bean = Gson().fromJson(json, HistoryBean::class.java)
                if (1 == bean.status) {
                    baseContext.runOnUiThread {
                        (baseContext as SocialInsuranceA).setTime(bean.data.start, bean.data.end)
                        mData1.addAll(bean.data.list)
                        mData2.addAll(bean.data.list)
                        mData3.addAll(bean.data.list)
                        mAdapter1.notifyDataSetChanged()
                        mAdapter2.notifyDataSetChanged()
                        mAdapter3.notifyDataSetChanged()
                        if (mData1.size == 0) {
                            showToast("该时间段内未查询到记录")
                        }
                    }
                } else {
                    UIUtils.showToastSafely(bean.msg)
                }
            }
        })
    }*/

    inner class SocialAdapter1 : CommonAdapter<QyzgylxBean>(
        baseContext,
        R.layout.item_social1,
        qyzgBean
    ) {
        override fun convert(holder: ViewHolder?, t: QyzgylxBean?, position: Int) {
            holder?.setText(R.id.time, qyzgBean[position].riqi)
            holder?.setText(R.id.company, qyzgBean[position].gsmc)
            holder?.setText(R.id.group, qyzgBean[position].gsjn)
            holder?.setText(R.id.person, qyzgBean[position].grjn)
            holder?.setText(R.id.income, qyzgBean[position].zhhr)
        }
    }

    inner class SocialAdapter2 : CommonAdapter<GsbxBean>(
        baseContext,
        R.layout.item_social2,
        gsBean
    ) {
        override fun convert(holder: ViewHolder?, t: GsbxBean?, position: Int) {
            holder?.setText(R.id.time, gsBean[position].riqi)
            holder?.setText(R.id.company, gsBean[position].gsmc)
            holder?.setText(R.id.group, gsBean[position].zhhr)
        }
    }

    inner class SocialAdapter3 : CommonAdapter<SybxBean>(
        baseContext,
        R.layout.item_social3,
        syBean
    ) {
        override fun convert(holder: ViewHolder?, t: SybxBean?, position: Int) {
            holder?.setText(R.id.company, syBean[position].gsmc)
            holder?.setText(R.id.time, syBean[position].riqi)
            holder?.setText(R.id.group, syBean[position].gsjn)
            holder?.setText(R.id.person, syBean[position].grjn)
        }
    }

}